package com.zhongwang.mms.module.basic.controller;

import com.zhongwang.mms.base.BaseController;
import com.zhongwang.mms.common.JsonResult;
import com.zhongwang.mms.common.ValidResult;
import com.zhongwang.mms.gen.entity.TDept;
import com.zhongwang.mms.gen.entity.TEmploy;
import com.zhongwang.mms.module.basic.service.TDeptService;
import com.zhongwang.mms.module.basic.service.TEmployService;
import com.zhongwang.mms.module.system.log.LogService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


/**
 * @Description: 员工维护 控制层
 * @Author: China.wsg
 * @Date: 2020/06/03 16:23
 */
@Controller
@RequestMapping("/basic/employ")
public class TEmployController extends BaseController {

    @Autowired
    private TEmployService employService;
    @Autowired
    private LogService logService;
    @Autowired
    private TDeptService deptService;

    /**
     * 员工 页面
     *
     * @return 页面路径
     */
    @GetMapping("/page")
    @RequiresPermissions({"basic:employ:view"})
    public String page(Model model) {
        model.addAttribute("dept", deptService.findDeptInfo(new TDept()));
        return "basic/employ/page";
    }

    /**
     * 员工列表
     *
     * @return 员工 Json 数据
     */
    @ResponseBody
    @PostMapping("/list")
    @RequiresPermissions({"basic:employ:view"})
    public List<TEmploy> list(TEmploy employ) {
        return employService.findEmploy(employ);
    }

    /**
     * 创建员工
     *
     * @param employ 员工信息
     * @return json 数据
     */
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions({"basic:employ:edit"})
    public JsonResult save(TEmploy employ) {
        logService.saveLog("新建员工", employ.getEmpName());
        return jsonResult(employService.save(employ));
    }

    /**
     * 删除员工
     *
     * @param employ 员工信息
     * @return json 数据
     */
    @ResponseBody
    @PostMapping("/del")
    @RequiresPermissions({"basic:employ:edit"})
    public JsonResult del(TEmploy employ) {
        JsonResult jsonResult = new JsonResult();
        String msg = employService.del(employ);
        jsonResult.setType(JsonResult.ResultType.SUCCESS);
        jsonResult.setMsg(msg);
        return jsonResult;
    }

    /**
     * 查询员工
     *
     * @param id 员工id
     * @return 员工
     */
    @ResponseBody
    @PostMapping("/id")
    @RequiresPermissions({"basic:employ:edit"})
    public TEmploy id(Integer id) {
        return employService.findEmployById(id);
    }

    /**
     * 修改员工
     *
     * @param employ 员工信息
     * @return json 数据
     */
    @ResponseBody
    @PostMapping("/update")
    @RequiresPermissions({"basic:employ:edit"})
    public JsonResult update(TEmploy employ) {
        TEmploy r = employService.findEmployById(employ.getEmpId());
        logService.saveLog("修改员工", r, employ);
        return jsonResult(employService.updatEmploy(employ));
    }


}
